package com.dominator.service.impl;

import com.dominator.enums.ReqEnums;
import com.dominator.mybatis.dao.T_userDao;
import com.dominator.mybatis.dao.T_userPO;
import com.dominator.service.UserService;
import com.dominator.utils.api.ApiMessage;
import com.dominator.utils.dao.RedisUtil;
import com.dominator.utils.exception.ApiException;
import com.dominator.utils.system.PrimaryGenerater;
import com.dominFramework.core.typewrap.Dto;
import com.dominFramework.core.typewrap.impl.HashDto;
import com.dominator.utils.api.ApiMessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    private static RedisUtil ru = RedisUtil.getRu();

    @Autowired
    private T_userDao t_userDao;

    /**
     * 根据手机号检查用户是否存在
     *
     * @param dto phone String 必传 手机号
     * @return
     */
    @Override
    public ApiMessage checkExist(Dto dto) {
        dto.put("is_valid", "1");
        int count = t_userDao.rows(dto);
        return ApiMessageUtil.success(count);
    }

    /**
     * 注册
     *
     * @param dto phone String 必传 11位手机号
     *            nickname String 必传 昵称
     *            head_img_url String 非必传 头像url
     *            gender String 非必传 0女 1男
     * @return
     */
    @Override
    @Transactional
    public ApiMessage register(Dto dto) throws ApiException {
        try {
            String user_id = PrimaryGenerater.getInstance().uuid();
            String phone = dto.getString("phone");
            T_userPO user = new T_userPO();
            user.copyProperties(dto);
            user.setId(user_id);
            user.setCreate_time(new Date());
            user.setIs_valid("1");
            int count = 0;
            count = t_userDao.insert(user);
            if (count != 1) {
                throw new ApiException(ReqEnums.REQ_UPDATE_ERROR.getCode(), ReqEnums.REQ_UPDATE_ERROR.getMsg() + "user");
            }

            T_userPO user1 = t_userDao.selectByKey(user_id);
            ru.setObject("user_" + phone, user1);

            return ApiMessageUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ApiException(ReqEnums.REQ_UPDATE_ERROR.getCode(), ReqEnums.REQ_UPDATE_ERROR.getMsg());
        }
    }

    /**
     * 获取用户信息
     * @param dto phone String 必传 手机号
     * @return
     */
    @Override
    public ApiMessage getUser(Dto dto) {
        dto.put("is_valid", "1");
        T_userPO user = t_userDao.selectOne(dto);
        Dto resDto = new HashDto();
        resDto.put("user", user);
        return ApiMessageUtil.success(resDto);
    }

    /**
     * 修改用户信息
     *
     * @param dto user_id String 必传 用户id
     *            nickname String 必传 昵称
     *            head_img_url String 非必传 头像url
     *            gender String 非必传 0女 1男
     * @return
     */
    @Override
    @Transactional
    public ApiMessage editUser(Dto dto) throws ApiException {
        try {
            String user_id = dto.getString("user_id");
            T_userPO user = new T_userPO();
            user.copyProperties(dto);
            user.setId(user_id);
            user.setModify_time(new Date());
            int count = 0;
            count = t_userDao.updateByKey(user);
            if(count != 1){
                throw new ApiException(ReqEnums.REQ_UPDATE_ERROR.getCode(), ReqEnums.REQ_UPDATE_ERROR.getMsg());
            }
            T_userPO user1 = t_userDao.selectByKey(user_id);
            ru.setObject("user_" + user1.getPhone(), user1);
            return ApiMessageUtil.success(count);
        } catch (ApiException e) {
            e.printStackTrace();
            throw new ApiException(ReqEnums.REQ_UPDATE_ERROR.getCode(), ReqEnums.REQ_UPDATE_ERROR.getMsg());
        }
    }
}
